package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimail.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.BrandDao;
import com.atguigu.gulimail.product.entity.BrandEntity;
import com.atguigu.gulimail.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        //select * from xx where id like x or bb like x;
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.like("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void updateDetil(BrandEntity brand) {
        //首先先更新本表
        this.updateById(brand);
        //判断brand_name是否为null  如果不为空  同步其他关联表的数据
        if (!StringUtils.isEmpty(brand.getName())) {

            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
            //TODO  还有其他关联表要更新
        }

    }

    @Override
    public void removeByIdsWithRelation(List<Long> brandIds) {
        //首先先删除自己
        this.baseMapper.deleteBatchIds(brandIds);
        //然后再删除关系表

        brandIds.forEach(item -> {
            categoryBrandRelationService.remove(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", item));
        });
    }
//com.atguigu.gulimail.product.vo
}